<?php
/*--------------------------------

INSERT INTO `fanwe_sms` VALUES ('0', 'Wzt', '温州团定制短信接口', '', '21111', '1', '1', '', '0');
--------------------------------*/
include_once("Sms.class.php");

class WztSms implements Sms
{

	public $message = "";
	public $smsInfo;
	public $db_wzt;
	
    public function __construct($smsInfo = '')
    { 	
		if(!empty($smsInfo))
		{
			set_time_limit(0);
			
			$this->smsInfo = $smsInfo;
		}
		
		require_once(ROOT_PATH.'app/source/class/mysql_db.php');
		
		$this->db_wzt = new mysql_db("211.140.81.92:3306", "11","d@","dd",'utf8',0);
    }
	
	public function sendSMS($mobiles=array(),$content,$sendTime='')
	{
		$mobileLen = 2000;
		$mobileList = array_chunk($mobiles,$mobileLen);
		
		$contentLen = mb_strlen($content);
		$smsTotalCount = ceil($contentLen / 160) * count($mobiles);		
		$successNum = 0;
		$code = '';
		foreach($mobileList as $mobileItem)
		{
			$mobile = implode(",",$mobileItem);
				
			$sql = "insert into api_mt_wzt(SM_ID,SRC_ID,MOBILES,CONTENT,IS_WAP,URL,SEND_TIME,SM_TYPE,MSG_FMT,TP_PID,TP_UDHI,FEE_TERMINAL_ID,FEE_TYPE,FEE_CODE,FEE_USER_TYPE)"
				   ." values "
  				   ."(0, 3, '{$mobile}', '{$content}', 0, '', null, 0, 0, 0, 0, '', '', '', 0)";	
			//echo $sql;
  			$this->db_wzt->query($sql);
			$rs = $this->db_wzt->affected_rows();	
			$smsLog['send_content'] = $content;
			$sendCount = count($mobileItem);				
			if($rs > 0)
			{
				$smsLog['action_message'] = '提交短信成功';
				$smsLog['success_mobiles'] = $mobile;
				$smsLog['fail_mobiles'] = "";
				$smsLog['success_count'] = $sendCount;
				$smsLog['fail_count'] = 0;
				$smsLog['expense_count'] = ceil($contentLen / 160) * $sendCount;
				$successNum += $sendCount;
			}
			else
			{
				$smsLog['action_message'] = '提交短信失败';
				$smsLog['success_mobiles'] = "";
				$smsLog['fail_mobiles'] = $mobile;
				$smsLog['success_count'] = 0;
				$smsLog['fail_count'] = $sendCount;
				$smsLog['expense_count'] = 0;
			}
				
			$smsLog['send_time'] = a_gmtTime();
				
			if(intval(a_fanweC('SMS_SEND_LOG')) == 1)
			{
				$sql = "insert into ".$GLOBALS['db_config']['DB_PREFIX']."sms_send_log(class_name,send_content,success_count,success_mobiles,fail_mobiles,expense_count,fail_count,action_message,send_time) values('Wzt','".$smsLog['send_content']."','".$smsLog['success_count']."','".$smsLog['success_mobiles']."','".$smsLog['fail_mobiles']."','".$smsLog['expense_count']."','".$smsLog['fail_count']."','".$smsLog['action_message']."','".$smsLog['send_time']."')";					
				$GLOBALS['db']->query($sql);
			}
		}
		
		if(($code == "100" && count($mobiles) == 1) || ($smsTotalCount == $successNum))
		{
			$this->message ="成功发送短信【".$content."】，到手机".implode(",",$mobiles);
			return 1;
		}
		else
		{
			$this->message = $smsTotalCount."条短信中，有".($smsTotalCount - $successNum)."条未成功发送到手机".implode(",",$mobiles);
			return 0;			
		}
	}
	
}
?>